<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="阶段" prop="stage">
        <el-select v-model="queryParams.stage" placeholder="请选择阶段" clearable>
          <el-option
            v-for="dict in dict.type.sys_stage"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="知识点" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入知识点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重要性" prop="importance">
        <el-select v-model="queryParams.importance" placeholder="请选择重要性" clearable>
          <el-option
            v-for="dict in dict.type.ya_importance"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teaching:knowledge_points:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teaching:knowledge_points:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teaching:knowledge_points:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teaching:knowledge_points:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="importOpen=true"
          v-hasPermi="['teaching:knowledge_points:export']"
        >批量导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="knowledge_pointsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="知识点" align="center" prop="name" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="上一次更新人" align="center" prop="updateBy" />
      <el-table-column label="重要性" align="center" prop="importance">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ya_importance" :value="scope.row.importance"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teaching:knowledge_points:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teaching:knowledge_points:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改高频面试题对话框 -->
    <el-dialog :title="title" :visible.sync="open" fullscreen  append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" >
        <el-form-item label="阶段" prop="stage">
          <el-select v-model="form.stage" placeholder="请选择阶段">
            <el-option
              v-for="dict in dict.type.sys_stage"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="知识点" prop="name">
          <el-input v-model="form.name" placeholder="请输入内容" />
          <!--          <mavon-editor v-model="form.name" defaultOpen="preview" :editable="true" :toolbarsFlag="true" />-->
        </el-form-item>
        <el-form-item label="为什么要学习" prop="why">
<!--          <mavon-editor v-model="form.why" defaultOpen="preview" :editable="true" :toolbarsFlag="true" />-->
          <editor v-model="form.why" :min-height="192"/>
        </el-form-item>
        <el-form-item label="是什么" prop="what">
<!--          <mavon-editor v-model="form.what" defaultOpen="preview" :editable="true" :toolbarsFlag="true" />-->
<!--          <el-input v-model="form.what" type="textarea" placeholder="请输入内容" />-->
          <editor v-model="form.what" :min-height="192"/>
        </el-form-item>
        <el-form-item label="什么场景用" prop="whenUse">
<!--          <mavon-editor v-model="form.whenUse" defaultOpen="preview" :editable="true" :toolbarsFlag="true" />-->
          <editor v-model="form.whenUse" :min-height="192"/>
        </el-form-item>
        <el-form-item label="怎么用" prop="how">
<!--          <mavon-editor v-model="form.how" defaultOpen="preview" :editable="true" :toolbarsFlag="true" />-->
          <editor v-model="form.how" :min-height="192"/>
        </el-form-item>
        <el-form-item label="总结" prop="summary">
<!--          <mavon-editor v-model="form.summary" defaultOpen="preview" :editable="true" :toolbarsFlag="true" />-->
          <editor v-model="form.summary" :min-height="192"/>
        </el-form-item>
        <el-form-item label="高频面试题">
          <editor v-model="form.highInterviewQuestion" :min-height="192"/>
        </el-form-item>

        <el-form-item label="重要性" prop="importance">
          <el-select v-model="form.importance" placeholder="请选择重要性">
            <el-option
              v-for="dict in dict.type.ya_importance"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--导入对话框-->
    <el-dialog title="批量导入" :visible.sync="importOpen" fullscreen  append-to-body>
      <el-form ref="importForm"  label-width="100px" >
        <el-form-item label="" prop="stage">
          <el-upload
            class="upload-demo"
            action="http://localhost:82/dev-api/teaching/knowledge_points/import"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :limit="10"
            :on-exceed="handleExceed"
            :on-success="handleSuccess"
            :file-list="fileList" :headers="headers">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传md文件</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="" prop="stage">
          <!--上传统计 -->
          <div>
            <span style="color: #67C23A">新增：{{uploadDetail.newList.length}}</span>
            <span style="color: #F56C6C">更新：{{uploadDetail.updateList.length}}</span>
          </div>
          <!--新增列表 newList-->
          <div v-if="uploadDetail.newList.length>0">
            <h3>新增列表</h3>
            <ul class="success-list">
              <li v-for="(name, index) in uploadDetail.newList" :key="name">{{index + 1}}. {{name}}</li>
            </ul>
          </div>
          <!--更新列表 updateList-->
          <div v-if="uploadDetail.updateList.length>0">
            <h3>更新列表</h3>
            <ul class="success-list">
              <li v-for="(name, index) in uploadDetail.updateList" :key="name">{{index + 1}}. {{name}}</li>
            </ul>
          </div>

        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listKnowledge_points, getKnowledge_points, delKnowledge_points, addKnowledge_points, updateKnowledge_points } from "@/api/teaching/knowledge_points";
import {getToken} from "@/utils/auth";

export default {
  name: "Knowledge_points",
  dicts: ['sys_stage', 'ya_importance'],
  data() {
    return {
      uploadDetail:{
        newList:[],
        updateList:[]
      },
      headers:{"Authorization":"Bearer "+ getToken()},
      fileList: [/*{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}*/],
      // 是否显示弹出层
      importOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 高频面试题表格数据
      knowledge_pointsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stage: null,
        name: null,
        why: null,
        what: null,
        whenUse: null,
        how: null,
        summary: null,
        highInterviewQuestion: null,
        importance: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stage: [
          { required: true, message: "阶段不能为空", trigger: "change" }
        ],
        name: [
          { required: true, message: "知识点不能为空", trigger: "blur" }
        ],
        importance: [
          { required: true, message: "重要性不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSuccess(res, file) {
      this.uploadDetail.newList=res.data.newList;
      this.uploadDetail.updateList=res.data.updateList;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },
    /** 查询知识点列表 */
    getList() {
      this.loading = true;
      listKnowledge_points(this.queryParams).then(response => {
        this.knowledge_pointsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stage: null,
        name: null,
        why: null,
        what: null,
        whenUse: null,
        how: null,
        summary: null,
        highInterviewQuestion: null,
        importance: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加知识点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getKnowledge_points(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改知识点";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateKnowledge_points(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addKnowledge_points(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除知识点编号为"' + ids + '"的数据项？').then(function() {
        return delKnowledge_points(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teaching/knowledge_points/export', {
        ...this.queryParams
      }, `knowledge_points_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.success-list {
  display: flex;
  flex-wrap: wrap;
  list-style-type: none;
  padding: 0;
}

.success-list li {
  background-color: #f0f9eb;
  border: 1px solid #e1f3d8;
  margin: 3px;
  padding: 6px;
  border-radius: 4px;
  color: #67c23a;
  flex: 0 0 18%;
  box-sizing: border-box;
}
</style>
